import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;

/**
 * Collection接口的常用方法
 *
 * @author wx
 * @create 2019-12-13 22:09
 */
public class CommonMethods {

    public static void main(String[] args) {
        Collection<Object> coll = new ArrayList<Object>();
        Collection<Object> subColl = new ArrayList<Object>();

        //1.add/addAll
        coll.add("abc");
        subColl.add(4);

        subColl.add("zzz");
        coll.addAll(subColl);
        //2.size
        System.out.println("coll.size() = " + coll.size() + "subCol.size() = " + subColl.size());
        //3.clear
        subColl.clear();
        System.out.println("coll.size() = " + coll.size() + "subCol.size() = " + subColl.size());

        //4.isEmpty
        System.out.println("coll.isEmpty() = " + coll.isEmpty());
        System.out.println("subColl.isEmpty() = " + subColl.isEmpty());

        //5.contains/containsAll
        //如果是不同的自定义对象的话，是判断他的equals()
        coll.contains("zzz");//判断单个集合是否包含？
        coll.contains(subColl);//判断空集合是否包含？：
        subColl.add("zzz");
        subColl.add(4);
        coll.containsAll(subColl);

        coll.add(new String("aaa"));
        //不同的String对象是否contains?
        String aaa = new String("aaa");
        System.out.println("coll.contains(aaa) = " + coll.contains(aaa));

        //自定义对象呢？
        //重写自定义对象的euqals方法，告诉对象什么时候算相等，这样即使不是一个对象也可以返回true
        coll.add(new Person("wx", 25));
        Person wx = new Person("wx", 25);
        System.out.println("coll.contains(wx) = " + coll.contains(wx));

        //6.remove/removeAll
        //不同对象也是可以remove成功的
        System.out.println("coll.remove(aaa) = " + coll.remove(aaa));


        //7.equals
        coll.add(new String("aaa"));
        System.out.println("coll.equals(subColl) = " + coll.equals(subColl));


        //8.hashCode
        System.out.println("coll.hashCode() = " + coll.hashCode());
        //9.iterator
        Iterator<Object> iterator = coll.iterator();
        while (iterator.hasNext()) {
            System.out.println("iterator.next() = " + iterator.next());
        }

        //10.toArray
        for (Object o : coll.toArray()) {
            System.out.println("o = " + o);
        }
    }
}

class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        System.out.println("调用类的equals方法..");
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age &&
                Objects.equals(name, person.name);
    }

//    @Override
//    public int hashCode() {
//        return Objects.hash(name, age);
//    }
}
